<?php
namespace app\common\model;
use think\model\concern\SoftDelete;
class Message extends \think\Model
{
    protected $resultSetType = 'collection';
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    //关联跟进信息
    public function followInfo()
    {
        return $this->hasMany('Follow','message_id','id')->order('date desc');
    }

    //关联到访信息
    public function visitInfo()
    {
        return $this->hasMany('Visit','message_id','id')->order('date desc');
    }

    public function followInfoEnd()
    {
        return $this->hasOne('Follow','message_id','id');
    }

    //关联上传凭证信息
    public function dealInfo()
    {
        return $this->hasMany('Deal','message_id','id');
    }

    //关联项目信息
    public function buildingInfo()
    {
        return $this->hasOne('Building','id','building')->field('id,name,company');
    }

    //渠道轮询信息(倒叙)
    public function turnsInfo(){
        return $this -> hasOne('turns','message_id','id')->order('id');
    }

    //渠道轮询信息(正叙)
    public function turnsInfoOrder(){
        return $this -> hasOne('turns','message_id','id')->order('id desc');
    }

    //所有轮训记录
    public function allTurns(){
        return $this -> hasMany('turns','message_id','id')->order('id');
    }

    //跟进人信息
    public function accountInfo(){
        return $this -> hasOne('Account','id','account_id') ->removeOption('soft_delete');
    }

    public function transferInfo(){
        return $this -> hasOne('transfer','message_id','id');
    }

    //添加客户信息
    public function inserts($loginInfo){
        $data = input('param.');
        $data['account_id'] = $loginInfo['id'];
        $validate = validate($this -> name);
        if(!$validate->check($data))
            return [
                'message' => $validate->getError()
            ];


        $saveData['name'] = $data['name'];
        $saveData['telephone'] = $data['telephone'];
        $saveData['sex'] = $data['sex'];
        $saveData['building'] = $data['building'];
        $saveData['house1'] = intval($data['house1']);
        $saveData['house2'] = intval($data['house2']);
        $saveData['house3'] = intval($data['house3']);
        $saveData['house4'] = intval($data['house4']);
        $saveData['price'] = $data['price'];
        $saveData['number'] = $data['number'];
        $saveData['notes'] = $data['notes'];
        $saveData['status'] = $data['status'];


        if($data['id']){
            //判断是否非法操作
            $userInfo = $this -> where('id',$data['id'])->find();
            if (!in_array($loginInfo['id'],[$userInfo['account_id'],$userInfo['transferInfo']['to_account_id']]))
                return [
                    'message' => '无权修改该用户'
                ];

            //更新客户数据
            $this -> where('id','eq',$data['id']) -> update($saveData);

            if ($saveData['status'] =='已定'){
                //插入通告
                $check = \app\common\model\Inform::where('message_id',$data['id'])->find();
                if(!$check){
                    \app\common\model\Inform::create([
                        'name'  =>  $loginInfo['name'],
                        'offer'  =>  $loginInfo['roleInfo']['rolename'],
                        'amount' =>  $data['price'],
                        'date' => time(),
                        'message_id' => $data['id']
                    ]);
                    $mark = 1;

                    //发送小程序通知
                    $allOpenId = \app\common\model\Account::where('open_id','neq',null)->column('open_id');
                    $allOpenId = array_unique($allOpenId);
                    $WxMini =new \app\common\logic\WxMini();
                    $data = [
                        'keyword1' => ['value' => '用户成交喜报'],
                        'keyword2' => ['value' => $loginInfo['name']],
                        'keyword3' => ['value' => "喜报，用户成交了价格$data[price]元的房子"],
                        'keyword4' => ['value' => date('Y-m-d H:i:s')],
                    ];
                    foreach ($allOpenId as $value){
                        $WxMini -> sendNotify($value,3,'pages/bought/bought',$data);
                    }
                }
            }
            return [
                'code' => '1',
                'message' => '更新成功',
                'mark' => $mark?:0,
            ];
        }else{
            $saveData['account_id'] = $loginInfo['id'];
            //新增客户数据
            $saveData['time'] = time();
            $saveData['schedule'] = time();
            $userInfo = $this -> create($saveData);
            $id = $userInfo -> id;

            //如果是渠道加入倒计时
            if (in_array($loginInfo['role'],['22','23','26','27']))
                \app\common\model\Turns::create([
                    'message_id'  =>  $id,
                    'account_id' =>  $loginInfo['id'],
                    'date' => time()
                ]);

            return [
                'code' => '1',
                'message' => '新增成功',
                'id' => $id
            ];
        }

    }

    //删除当前客户信息
    public function dels(){
        $data = input('param.');
        \app\common\model\Message::destroy($data['id']);
        return [
            'code' => '1',
            'message' => '删除成功',
        ];
    }

}